{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d78f4f5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime as dt\n",
    "from gs_quant.markets.index import Index\n",
    "from gs_quant.session import Environment, GsSession"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b50c33ff-4e90-4ba3-828c-1ca0f06e05f0",
   "metadata": {},
   "source": [
    "## Pre Requisites\n",
    "To use below functionality on **STS Indices**, your application needs to have access to the following datasets:\n",
    "1. [STSCONSTITUENTS](https://marquee.gs.com/s/developer/datasets/STSCONSTITUENTS) - Bottom level index constituents and associated weights\n",
    "\n",
    "You can request access by going to the Dataset Catalog Page linked above.\n",
    "\n",
    "Note - Please skip this if you are an internal user"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6eff76d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('read_product_data',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ac14d1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = Index.get('GSXXXXXX')  # substitute input with any identifier for an index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61adac51",
   "metadata": {},
   "source": [
    "## Get Constituents\n",
    "\n",
    "Returns a pandas dataframe of the constituents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "109fef12",
   "metadata": {},
   "outputs": [],
   "source": [
    "index.get_latest_constituents()      # returns the latest constituents of the index."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6cf3b37",
   "metadata": {},
   "outputs": [],
   "source": [
    "index.get_constituents_for_date(dt.date(2021, 7, 1))      # returns the constituents of the index for the specified date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da4c497a",
   "metadata": {},
   "outputs": [],
   "source": [
    "index.get_constituents(dt.date(2021, 6, 1), dt.date(2021, 6, 10))     # returns the constituents sets of the index for the specified date range"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69d5708d",
   "metadata": {},
   "source": [
    "## Get Constituents as Instruments\n",
    "\n",
    "Returns the constituents as Instrument objects"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0644e0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "index.get_latest_constituent_instruments()      # returns the latest constituents of the index as instrument objects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18d10cd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "index.get_constituent_instruments_for_date(dt.date(2021, 7, 1))      # returns the constituents of the index for the specified date as Instrument objects"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79a818b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "index.get_constituent_instruments(dt.date(2021, 6, 1), dt.date(2021, 6, 10))     # returns the constituents sets of the index for the specified date range as Instrument objects"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d66e679",
   "metadata": {},
   "source": [
    "*Have any other questions? Reach out to the [Marquee STS team](mailto:gs-marquee-sts-support@gs.com)!*"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}